package com.yuncommunity.cloudlab.controller.admin

import com.google.gson.Gson
import com.yuncommunity.cloudlab.model.User
import com.yuncommunity.cloudlab.repository.UserRepository
import com.yuncommunity.cloudlab.utils.MyCaptcha
import com.yuncommunity.cloudlab.utils.MyResult
import net.bytebuddy.utility.RandomString
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.util.DigestUtils
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping("admin/api/user")
@CrossOrigin("*")
class AdminUser {
    @Autowired
    lateinit var userRepository: UserRepository

    /**
     * 获取图形验证码
     *
     * @return
     */
    @GetMapping("/imageCode")
    fun imageCode(): MyResult {
        return MyResult(MyCaptcha.newImageCaptcha())
    }

    @GetMapping("/login")
    @ResponseBody
    fun login(username: String, password: String, captchaId: String, imageCode: String, request: HttpServletRequest): MyResult {
        if (!MyCaptcha.imageCodeVerify(captchaId, imageCode)) {
            return MyResult(3, "验证码错误")
        }
        var u = userRepository.findByNameAndPassword(username, DigestUtils.md5DigestAsHex(password.toByteArray()))
        return if (u == null) {
            MyResult(1, "账号或密码错误")
        } else {
            u.token = RandomString.make(32)
            userRepository.save(u)
            u.password = ""
            MyResult(0, u)
        }
    }

    @GetMapping("/change_pwd")
    fun changePwd(token: String, new_pwd: String, old_pwd: String): MyResult {
        var u = userRepository.findByToken(token)
        if (u == null) {
            return MyResult(-1, "登录已过期,请重新登录")
        }
        if (DigestUtils.md5DigestAsHex(old_pwd.toByteArray()) != u.password) {
            return MyResult(1, "密码错误")
        }
        u.password = DigestUtils.md5DigestAsHex(new_pwd.toByteArray())
        userRepository.save(u)
        return MyResult("修改成功")
    }

    @GetMapping("/info")
    @ResponseBody
    fun info(token: String): MyResult {
        var u = userRepository.findByToken(token)
        return if (u == null) {
            MyResult(-1, "登录已过期,请重新登录")
        } else {
            u.password = ""
            MyResult(0, u)
        }
    }

    @GetMapping("/delete_by_id")
    fun deleteById(id: Long): MyResult {
        var user = userRepository.findById(id).orElse(null)
        if (user != null) {
            user.isDelete = true
            userRepository.save(user)
        }
        return MyResult(0, "success")
    }

    @GetMapping("/delete_by_ids")
    fun deleteByIds(ids: String): MyResult {
        var idList = ids.split(",")
        var userIdList = arrayListOf<Long>()
        for (id in idList) {
            var u = User()
            u.id = id.toLong()
            userIdList.add(u.id)
        }
        userRepository.updateDelete(userIdList)
        return MyResult("删除成功")
    }
}